<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <FilterTable
        ref="table"
        :columns="columns"
        columnsRef="partAccessDesignTabsBrandModal"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'rows'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="searchDetail"
      >
        <template slot="moreActions">
          <el-button size="small" type="primary" icon="el-icon-plus">新增</el-button>
        </template>
      </FilterTable>
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { partQueryDetails, partItemQuery, partStockingSave } from '@/api/parts/access/design';

export default {
  name: 'BrandModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: {
        stockTakingNo: ''
      }
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      arr: [],
      formList: this.createFormList(),
      fetchApi: partQueryDetails,
      fetchParams: {
        stockTakingNo: this.rowData.stockTakingNo
      },
      columns: this.createFormCols()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
    this.getdetail();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.brandId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    async getdetail() {
      console.log('1111111', this.rowData);
      let res = await partItemQuery({
        stockTakingNo: this.rowData.stockTakingNo
      });
      let map = res[0];
      let arr = ['', ''];
      for (let i in map) {
        if (i == 'planBegin' || i == 'planEnd') {
          if (i == 'planBegin') {
            arr[0] = map[i];
          } else {
            arr[1] = map[i];
          }
          if (arr[0] != '' && arr[1] != '') {
            console.log(arr);
            for (let k = 0; k < this.formList.length; k++) {
              if ('planBegin|planEnd' == this.formList[k].fieldName) {
                console.log(arr, '2222222');
                this.formList[k].initialValue = arr;
              }
            }
          }
        }
        for (let j = 0; j < this.formList.length; j++) {
          if (i == this.formList[j].fieldName) {
            console.log(map[i]);
            this.formList[j].initialValue = map[i];
          }
        }
      }
      console.log(map, '获取数据');
    },
    createFormCols() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.deleteRow(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '时间范围',
          dataIndex: 'timeFrame',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提醒日期',
          dataIndex: 'remindDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选取仓库',
          dataIndex: 'wareHourse',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'wareHourseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '排序方式',
          dataIndex: 'sortBy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '入库开始日期',
          dataIndex: 'inStorageBegin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '入库结束日期',
          dataIndex: 'inStorageEnd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '出库开始日期',
          dataIndex: 'outStorageBegin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '出库结束日期',
          dataIndex: 'outStorageEnd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '每张盘点单配件个数',
          dataIndex: 'count',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '处理状态',
          dataIndex: 'handleStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '盘点计划编码',
          fieldName: 'stockTakingNo',
          placeholder: '请输入...',
          rules: [{ required: true, message: '盘点计划编码不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '盘点计划名称',
          fieldName: 'stockTakingName',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '盘点计划名称不能为空', trigger: 'change' }]
        },
        {
          type: 'RANGE_DATE',
          label: '计划周期',
          fieldName: 'planBegin|planEnd',
          initialValue: [],
          placeholder: '请选择',
          rules: [{ required: true, message: '计划周期不能为空', trigger: 'change' }],
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'SELECT',
          label: '计划类型',
          fieldName: 'planType',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '计划周期结束日期不能为空', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '盘点周期',
          fieldName: 'stockTakingCycle',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '盘点周期', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '提醒日为每周/每月第几天',
          fieldName: 'remindDate',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '提醒日', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '盘点类型',
          fieldName: 'stockTakingType',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '更新日期不能为空', trigger: 'change' }]
        }
      ];
    },
    searchDetail(row) {
      this.arr = [];
      if (row.length > 0) {
        for (let i = 0; i < row.length; i++) {
          this.arr.push(row[i]);
        }
      }
      console.log(this.arr);
    },
    saveData(val) {
      let data = {
        start: val.start,
        end: val.end,
        partNo: this.js.partNo,
        listDetailData: this.arr
      };
      this.fetchParams1 = Object.assign({}, data);
    },
    formChangeHandle(val) {
      console.log(val, '1111');
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      let data = Object.assign({}, this.saveData, { dms_table2: this.arr });
      console.log(data);
      if (data !== null) {
        let res = {};
        res = await partStockingSave(data);
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
